/**
 * 
 */
var thispage = {
		init: function(){
			this.tableInit();
			this.initObj();
			this.initEvents();
		},
		initObj: function(){
		},
		initEvents: function(){
			var t = this;
			$('.d-tbl .layui-btn').on('click', function() {
				var type = $(this).attr('@event');
				t.funs[type] ? t.funs[type].call(this) : '';
			});
		},
		funs: {
			add: function(){
				pub.openEdit("新增系统权限", ctx + "/permission/edit", [ '420px', '350px' ]);
			},
			//删除行
			editRow: function(rowId){
				pub.openEdit("修改系统权限", ctx + "/permission/edit?id=" + rowId, [ '420px', '350px' ]);
			},
			//编辑行
			delRow: function(rowId){
				layer.confirm('确定要删除吗', function(index) {
					layer.close(index);
					//向服务端发送删除指令
					$.post(ctx +"/permission/delete", {id: rowId}, function(ret){
						if(ret.state == "ok"){
							thispage.funs.search();
						}else{
							layer.alert(ret.msg || "服务器异常");
						}
					})
				});
			},
			search:  function(where){
				//执行重载
				$('#tblmain').bootstrapTable('refresh', {query: where || {}});
			},
			generator: function(){
				layer.confirm('确定要同步吗', function(index) {
					layer.close(index);
					$.post(ctx +"/permission/generator", function(ret){
						if(ret.state == "ok"){
							thispage.funs.search();
						}else{
							layer.alert(ret.msg || "服务器异常");
						}
					})
				});
			}
		},
		tableInit: function(){
			var t = this;
			var maxH = document.documentElement.clientHeight;
			var height = maxH;
			
			var columns = [{
				field: 'id',
				title: 'ID',
				visible: false
			},{
				field: 'permission',
				title: '权限标识符',
				sortable: true
			},{
				field: 'action_key',
				title: '功能url',
				sortable: true
			}, {
				field: 'action_desc',
				title: '功能描述',
				sortable: true
			}, {
				field: 'action_module',
				title: '功能模块',
				sortable: true
			}, {
				field: 'enable',
				title: '是否启用验权',
				align: "center",
				formatter: tableDefault.cellChekboxFmt,
				sortable: true
			}, {
				field: 'createtime',
				title: '创建时间',
				sortable: true
			}, {
				field: 'updatetime',
				title: '最后修改时间',
				sortable: true
			}, {
				field: "option",
				title: "操作",
				align: "center",
				formatter: function(value, row, rowIndex){
					return "<span class='option glyphicon glyphicon-edit' aria-hidden='true' onclick=thispage.funs.editRow('"+row.id+"') ></span>" +
						   "<span class='option glyphicon glyphicon-remove' aria-hidden='true' onclick=thispage.funs.delRow('"+row.id+"') ></span>";
				}
			}];
			var tableParams = $.extend(tableDefault.params, {
				url: ctx + '/permission/query',
				sortOrder: "asc",   //排序方式
				sortName: "action_key", //默认排序字段
				pageSize: 15,   //每页的记录行数（*）
				columns: columns,
				height: height,   //表格高，如果没有设置height属性，表格自动根据记录条数决定表格高度
				onDblClickRow: function(row, $elemen, field){
					thispage.funs.editRow(row.id);
				}
			});
			$('#tblmain').bootstrapTable(tableParams);
		}
}



layui.use([ 'layer' ], function() {
	var table = layui.table;
	var layer = layui.layer;
	thispage.init();
	//提供给editMenu 调用
	window.events = {
			search: function(){
				thispage.funs.search();
			}
	}
});
